package cn.tangsihai.mq._05_topic;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Coumerr2 {
    public static final String QQUEUE_TOPIC_NAME2 = "QQUEUE_TOPIC_NAME22";
    public static void main(String[] args) throws Exception{
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("115.159.88.63");
        Connection connection = factory.newConnection();
        // 创建通道
        Channel channel = connection.createChannel();

        // 一对多
        channel.basicQos(1);

        // 创建队列
        channel.queueDeclare(QQUEUE_TOPIC_NAME2, true, false, false, null);

        // 绑定队列到交换机
        channel.queueBind(QQUEUE_TOPIC_NAME2, Producer.EXCHANGE_TOPIC_NAME, "info.#");
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到了消息：" + new String(body));
                long deliveryTag = envelope.getDeliveryTag();
                channel.basicAck(deliveryTag, false);
            }
        };
        // 处理消息
        channel.basicConsume(QQUEUE_TOPIC_NAME2, false, consumer);
        System.out.println("消息接收成功");
    }
}
